package com.lanting.meetingserver.base;

import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.Date;

import org.apache.commons.io.output.ByteArrayOutputStream;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.annotations.XStreamOmitField;



public class BaseEntity {
	/**
	 * 最后一次修改人编号
	 * */
	@XStreamOmitField
	private int Modifier;
	
	
	/**
	 * 最后修改时间
	 * */
	@XStreamOmitField
	private Date ModifyDate;


	public int getModifier() {
		return Modifier;
	}


	public void setModifier(int modifier) {
		Modifier = modifier;
	}


	public Date getModifyDate() {
		return ModifyDate;
	}


	public void setModifyDate(Date modifyDate) {
		ModifyDate = modifyDate;
	}
	
	public String toXml() throws IOException{
		XStream xstream = new XStream();
		ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
		Writer writer = new OutputStreamWriter(outputStream,"UTF-8");
		writer.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
		xstream.processAnnotations(this.getClass());
		xstream.toXML(this,writer);
		return outputStream.toString("UTF-8");
	}
	
	
	public String toJSON(){
		GsonBuilder gb = new GsonBuilder();
		gb.excludeFieldsWithoutExposeAnnotation();
		Gson gson= gb.create();
		return gson.toJson(this);
		
	}
	
	/**
	 * 去掉列表的根标签
	 * @param implicitTag 根标签的别名
	 * @throws IOException 
	 * */
	public String toListXml(String implicitTag) throws IOException{
		XStream xstream = new XStream();
		ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
		Writer writer = new OutputStreamWriter(outputStream,"UTF-8");
		writer.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
		xstream.processAnnotations(this.getClass());
		xstream.addImplicitCollection(this.getClass(),implicitTag);
		xstream.toXML(this,writer);
		return outputStream.toString("UTF-8");
	}
	/**
	 * 去掉列表的根标签
	 * @param implicitTag 根标签的别名
	 * @throws IOException 
	 * */
	public String toGroupXml(Class<? extends BaseEntity> groupclass, String implicitTag) throws IOException{
		XStream xstream = new XStream();
		ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
		Writer writer = new OutputStreamWriter(outputStream,"UTF-8");
		writer.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
		xstream.processAnnotations(this.getClass());
		xstream.addImplicitCollection(groupclass,implicitTag);
		xstream.toXML(this,writer);
		return outputStream.toString("UTF-8");
	}
}
